home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / editor / j414src.arc / MACVERT.C < prev    next >
C/C++ Source or Header  |  1989-10-10  |  3KB  |  151 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. /* Macvert converts old style macro files to the new style.  The old
  9.    style macros were binary files, the new ones are text files suitable
  10.    for loading with the "source" command of JOVE. */
  11.  
  12. #include <stdio.h>
  13.  
  14. extern int
  15.     read(),
  16.     write();
  17.  
  18. static void
  19.     output_new_definition();
  20.  
  21. static int    mac_fd;
  22.  
  23. static void
  24. mac_io(fcn, ptr, nbytes)
  25. int    (*fcn)();
  26. char    *ptr;
  27. int    nbytes;
  28. {
  29.     int    nio;
  30.  
  31.     if ((nio = (*fcn)(mac_fd, ptr, nbytes)) != nbytes)
  32.         fprintf(stderr, "[Macro %s error: %d got %d]",
  33.              (fcn == read) ? "read" : "write",
  34.              nbytes,
  35.              nio);
  36. }
  37.  
  38. #define NEWWAY    1
  39. #define OLDWAY    0
  40.  
  41. static int    int_how = NEWWAY;
  42.  
  43. /* Formatting int's the old way or the new "improved" way? */
  44.  
  45. #if defined(vax) || defined(pdp11)
  46.  
  47. static long
  48. ntohl(x)
  49. register long x;
  50. {
  51.     return(    (((x >>  0) & 0377) << 24) |
  52.         (((x >>  8) & 0377) << 16) |
  53.         (((x >> 16) & 0377) <<  8) |
  54.         (((x >> 24) & 0377) <<  0) );
  55. }
  56.  
  57. #else
  58.  
  59. static long
  60. ntohl(x)
  61. register long x;
  62. {
  63.     return(x);
  64. }
  65.  
  66. #endif
  67.  
  68. static int
  69. int_fmt(i)
  70. int    i;
  71. {
  72.     if (int_how == NEWWAY) {
  73.         /* Note: using ntohl() for an int is not portable! */
  74.         return (int) ntohl((long) i);
  75.     }
  76.     return i;
  77. }
  78.  
  79. static void
  80. read_and_write_macros(filein)
  81. char    *filein;
  82. {
  83.     int    namelen,
  84.         bodylen,
  85.         tmp;
  86.     char    macname[256],
  87.         macbuf[1024];
  88.  
  89.     if ((mac_fd = open(filein, 0)) == -1)
  90.         fprintf(stderr, "Cannot open %s\n", filein);
  91.  
  92.     while (read(mac_fd, (char *) &tmp, sizeof tmp) == (sizeof tmp)) {
  93. retry:        bodylen = int_fmt(tmp);
  94.         if (bodylen <= 0 || bodylen > 10000) {
  95.             if (int_how == NEWWAY) {
  96.                 int_how = OLDWAY;
  97.                 goto retry;
  98.             } else {
  99.                 fprintf(stderr, "I don't think \"%s\" is an old style JOVE macro file\n", filein);
  100.                 exit(1);
  101.             }
  102.         }
  103.         mac_io(read, (char *) &namelen, (int) (sizeof namelen));
  104.         namelen = int_fmt(namelen);
  105.         mac_io(read, macname, namelen);
  106.         mac_io(read, macbuf, bodylen);
  107.         output_new_definition(macname, macbuf, bodylen);
  108.     }
  109. }
  110.  
  111. static void
  112. pr_putc(c)
  113. int    c;
  114. {
  115.     if (c == '\\' || c == '^')
  116.         putchar('\\');
  117.      else if (c < ' ' || c == '\177') {
  118.         putchar('^');
  119.         c = (c == '\177') ? '?' : (c + '@');
  120.     }
  121.     putchar(c);
  122. }
  123.  
  124. static void
  125. output_new_definition(name, body, bodylen)
  126. char    *name,
  127.     *body;
  128. int    bodylen;
  129. {
  130.     int    i;
  131.  
  132.     fprintf(stdout, "define-macro %s ", name);
  133.     for (i = 0; i < bodylen; i++)
  134.         pr_putc(body[i]);
  135.     putchar('\n');
  136. }
  137.  
  138. int
  139. main(argc, argv)
  140. int    argc;
  141. char    *argv[];
  142. {
  143.     if (argc != 2) {
  144.         fprintf(stderr, "usage: macvert <old-style-macro-file>\n");
  145.         exit(1);
  146.     }
  147.  
  148.     read_and_write_macros(argv[1]);
  149.     return 0;
  150. }
  151.